import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register/company',
    name: 'CompanyRegister',
    component: () => import('../views/CompanyRegister.vue')
  },
  {
    path: '/register/worker',
    name: 'WorkerRegister',
    component: () => import('../views/WorkerRegister.vue')
  },
  {
    path: '/',
    component: () => import('../components/Layout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '/home',
        name: 'Home',
        redirect: '/tasks'
      },
      {
        path: '/tasks',
        name: 'TaskList',
        component: () => import('../views/task/TaskList.vue')
      },
      {
        path: '/task/create',
        name: 'CreateTask',
        component: () => import('../views/task/CreateTask.vue')
      },
      {
        path: '/task/:id',
        name: 'TaskDetail',
        component: () => import('../views/task/TaskDetail.vue')
      },
      {
        path: '/workers',
        name: 'Workers',
        component: () => import('../views/worker/WorkerList.vue')
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue')
      },
      {
        path: '/job-filter',
        name: 'JobFilter',
        component: () => import('../views/worker/JobFilter.vue')
      },
      {
        path: '/messages',
        name: 'Messages',
        component: () => import('../views/message/MessageList.vue')
      },
      {
        path: '/company-info',
        name: 'CompanyInfo',
        component: () => import('../views/company/CompanyInfo.vue')
      },
      {
        path: '/analytics',
        name: 'Analytics',
        component: () => import('../views/analytics/AnalyticsDashboard.vue')
      },
      {
        path: '/wallet',
        name: 'Wallet',
        component: () => import('../views/wallet/WalletManagement.vue')
      },
      {
        path: '/support',
        name: 'Support',
        component: () => import('../views/support/CustomerSupport.vue')
      },
      // 管理员路由
      // {
      //   path: '/admin/dashboard',
      //   name: 'AdminDashboard',
      //   component: () => import('../views/analytics/AdminDashboard.vue'),
      //   meta: { requiresAdmin: true }
      // },
      {
        path: '/admin/users',
        name: 'UserManagement',
        component: () => import('../views/admin/UserManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: '/admin/tasks',
        name: 'TaskManagement',
        component: () => import('../views/admin/TaskManagement.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: '/admin/analytics',
        name: 'AnalyticsManagement',
        component: () => import('../views/admin/AnalyticsManagement.vue'),
        meta: { requiresAdmin: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userType = localStorage.getItem('userType')
  
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else if (to.meta.requiresAdmin && userType !== 'admin') {
    // 管理员权限检查
    next('/tasks')
  } else {
    next()
  }
})

export default router 